'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 按角色名查询玩家
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var heroConf = require('../../../../config/heroConf');
var Code = require('../../../../consts/code');
var dataApi = require('../../../../libs/dataApi');
var roleModel = require('../../../../models/roleModel');
var PlayerManager = require('../../../../libs/playerManager');
var friendService = require('../../../../services/friendService');

module.exports = function (msg, session, next) {
    var role_name = msg.role_name;
    if (!role_name) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var friend_role_id_list = [];
    var friend_list;
    async.waterfall(
        [
            function (cb) {
                roleModel.getRoleByNameKeyWord(role_name, cb);
            },
            function (role_list, cb) {
                _.each(role_list, function (role_item) {
                    friend_role_id_list.push(role_item.role_id);
                });
                friendService.getFriendList(friend_role_id_list, cb);
            },
            function (role_list, cb) {
                friend_list = role_list;
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    friend_list: friend_list
                }
            });
        }
    );
};
